package com.cheng.controller;

import com.cheng.pojo.Product;
import com.cheng.pojo.Product_team;
import com.cheng.pojo.User;
import com.cheng.service.ProductService;
import com.cheng.service.ProductTeamService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/merchandise")
public class MerchandiseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTeamService productTeamService;


    //查询商家商品
    @RequestMapping(value = "/querymerchantsproduct")
    public String queryMerchantsProduct(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                                     Model model,
                                     HttpSession session) {
        User user = (User) session.getAttribute("user");
        PageInfo<Product> pageInfo = productService.querymerchantsProduct(page,pageSize,user.getUserCode());
        model.addAttribute("products", pageInfo);
        return "merchandise_control";
    }

    //查询商家项目
    @RequestMapping(value = "/queryMerchantsTeamProduct")
    public String queryMerchantsTeamProduct(@RequestParam(value = "page", defaultValue = "1") int page,
                                        @RequestParam(value = "pageSize", defaultValue = "8") int pageSize,
                                        Model model,
                                        HttpSession session) {
        User user = (User) session.getAttribute("user");
        PageInfo<Product_team> pageInfo = productTeamService.querymerchantsTeamProduct(page,pageSize,user.getUserCode());
        model.addAttribute("products", pageInfo);
        return "merchandise_team";
    }




    //商家删除商品
    @RequestMapping("/merchantsDeleteProduct")
    public String merchantsDeleteProduct(String productcode){
        productService.deleteProduct(productcode);
        return "redirect:querymerchantsproduct";
    }

    //商家删除项目
    @RequestMapping("/merchantsDeleteTeamProduct")
    public String merchantsDeleteTeamProduct(String productcode){
        productTeamService.merchantsDeleteTeamProduct(productcode);
        return "redirect:queryMerchantsTeamProduct";
    }


    //商家添加商品
    @RequestMapping("/addmerchandise")
    public String addmerchandise(){
        return "addmerchandise";
    }


    //@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
    //批量上传CommonsMultipartFile则为数组即可
    @RequestMapping("/upload")
    public String fileUpload(HttpSession session,
                             String productcode,
                             String productname,
                             String productdetil,
//                             String producttime,
                             String productpath,
                             String productgo,
                             String touring_party_code,
                             String productdetils,
                             String priductaddress,
                             @RequestParam("productimg1") CommonsMultipartFile productimg1 ,
                             @RequestParam("productimg2") CommonsMultipartFile productimg2 ,
                             @RequestParam("productimg3") CommonsMultipartFile productimg3 ,
                             @RequestParam("productimg4") CommonsMultipartFile productimg4 ,
                             @RequestParam("productimg5") CommonsMultipartFile productimg5 ,
                             HttpServletRequest request,
                             String cprice,
                             String aprice,
                             String team_number,
                             String remain_number
                            ) throws IOException, ParseException {



        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取String类型的时间
        String createdate = sdf.format(date);
        System.out.println(createdate);

        SimpleDateFormat qw = new SimpleDateFormat("yyyy-MM-dd");
        Date qwer = qw.parse(createdate);
        System.out.println(qwer);



        User user = (User) session.getAttribute("user");






        //获取文件名 : file.getOriginalFilename();
        String uploadFileName1 = productimg1.getOriginalFilename();
        String uploadFileName2 = productimg2.getOriginalFilename();
        String uploadFileName3 = productimg3.getOriginalFilename();
        String uploadFileName4 = productimg4.getOriginalFilename();
        String uploadFileName5 = productimg5.getOriginalFilename();



        //如果文件名为空，直接回到首页！
        if ("".equals(uploadFileName1)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName2)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName3)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName4)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName5)){
            return "addmerchandise";
        }
        System.out.println("上传文件名 : "+uploadFileName1);
        System.out.println("上传文件名 : "+uploadFileName2);
        System.out.println("上传文件名 : "+uploadFileName3);
        System.out.println("上传文件名 : "+uploadFileName4);
        System.out.println("上传文件名 : "+uploadFileName5);



        //上传路径保存设置
        String path = "F:\\idea文件作业\\end001\\web\\images\\"+user.getUserCode()+"\\";
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址："+realPath);

        InputStream is1 = productimg1.getInputStream(); //文件输入流
        InputStream is2 = productimg2.getInputStream(); //文件输入流
        InputStream is3 = productimg3.getInputStream(); //文件输入流
        InputStream is4 = productimg4.getInputStream(); //文件输入流
        InputStream is5 = productimg5.getInputStream(); //文件输入流


        OutputStream os1 = new FileOutputStream(new File(realPath,uploadFileName1)); //文件输出流
        OutputStream os2 = new FileOutputStream(new File(realPath,uploadFileName2)); //文件输出流
        OutputStream os3 = new FileOutputStream(new File(realPath,uploadFileName3)); //文件输出流
        OutputStream os4 = new FileOutputStream(new File(realPath,uploadFileName4)); //文件输出流
        OutputStream os5 = new FileOutputStream(new File(realPath,uploadFileName5)); //文件输出流

        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is1.read(buffer))!=-1){
            os1.write(buffer,0,len);
            os1.flush();
        }
        while ((len=is2.read(buffer))!=-1){
            os2.write(buffer,0,len);
            os2.flush();
        }
        while ((len=is3.read(buffer))!=-1){
            os3.write(buffer,0,len);
            os3.flush();
        }
        while ((len=is4.read(buffer))!=-1){
            os4.write(buffer,0,len);
            os4.flush();
        }
        while ((len=is5.read(buffer))!=-1){
            os5.write(buffer,0,len);
            os5.flush();
        }
        os1.close();
        os2.close();
        os3.close();
        os4.close();
        os5.close();
        is1.close();
        is2.close();
        is3.close();
        is4.close();
        is5.close();

        String img01="images/"+user.getUserCode()+"/"+uploadFileName1;
        String img02="images/"+user.getUserCode()+"/"+uploadFileName2;
        String img03="images/"+user.getUserCode()+"/"+uploadFileName3;
        String img04="images/"+user.getUserCode()+"/"+uploadFileName4;
        String img05="images/"+user.getUserCode()+"/"+uploadFileName5;

        productService.addmerchandise(productcode,productname,productdetil,qwer,productpath,productgo,touring_party_code,productdetils,priductaddress,user.getUserCode(),img01,img02,img03,img04,img05,cprice,aprice,team_number,remain_number);







        return "addmerchandise";
    }

    //商家添加团购
    @RequestMapping("/addmerchandiseteam")
    public String addmerchandiseteam(){
        return "addmerchandiseteam";
    }


    //添加团购项目
    //@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
    //批量上传CommonsMultipartFile则为数组即可
    @RequestMapping("/upload01")
    public String fileUpload01(HttpSession session,
                             String productcode,
                             String productname,
                             String productdetil,
                             String productpath,
                             String productgo,
                             String touring_party_code,
                             String productdetils,
                             String priductaddress,
                             @RequestParam("productimg1") CommonsMultipartFile productimg1 ,
                             @RequestParam("productimg2") CommonsMultipartFile productimg2 ,
                             @RequestParam("productimg3") CommonsMultipartFile productimg3 ,
                             @RequestParam("productimg4") CommonsMultipartFile productimg4 ,
                             @RequestParam("productimg5") CommonsMultipartFile productimg5 ,
                             HttpServletRequest request,
                             String price,
                             String team_number,
                             String remain_number,
                             String group_number
    ) throws IOException, ParseException {



        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取String类型的时间
        String createdate = sdf.format(date);
        System.out.println(createdate);

        SimpleDateFormat qw = new SimpleDateFormat("yyyy-MM-dd");
        Date qwer = qw.parse(createdate);
        System.out.println(qwer);



        User user = (User) session.getAttribute("user");

        System.out.println("11111111111111111111111111111"+productcode);
        System.out.println("222222222222222222222222"+productname);
        System.out.println("333333333333333333333"+productdetil);
//        System.out.println("444444444444444444444444"+producttime);
        System.out.println("5555555555555555555555"+productpath);
        System.out.println("6666666666666666666666"+productgo);
        System.out.println("7777777777777777777777"+touring_party_code);
        System.out.println("888888888888888888888"+productdetils);
        System.out.println("99999999999999999999"+priductaddress);





        //获取文件名 : file.getOriginalFilename();
        String uploadFileName1 = productimg1.getOriginalFilename();
        String uploadFileName2 = productimg2.getOriginalFilename();
        String uploadFileName3 = productimg3.getOriginalFilename();
        String uploadFileName4 = productimg4.getOriginalFilename();
        String uploadFileName5 = productimg5.getOriginalFilename();



        //如果文件名为空，直接回到首页！
        if ("".equals(uploadFileName1)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName2)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName3)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName4)){
            return "addmerchandise";
        }
        if ("".equals(uploadFileName5)){
            return "addmerchandise";
        }
        System.out.println("上传文件名 : "+uploadFileName1);
        System.out.println("上传文件名 : "+uploadFileName2);
        System.out.println("上传文件名 : "+uploadFileName3);
        System.out.println("上传文件名 : "+uploadFileName4);
        System.out.println("上传文件名 : "+uploadFileName5);



        //上传路径保存设置
        String path = "F:\\idea文件作业\\end001\\web\\images\\"+user.getUserCode()+"\\";
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址："+realPath);

        InputStream is1 = productimg1.getInputStream(); //文件输入流
        InputStream is2 = productimg2.getInputStream(); //文件输入流
        InputStream is3 = productimg3.getInputStream(); //文件输入流
        InputStream is4 = productimg4.getInputStream(); //文件输入流
        InputStream is5 = productimg5.getInputStream(); //文件输入流


        OutputStream os1 = new FileOutputStream(new File(realPath,uploadFileName1)); //文件输出流
        OutputStream os2 = new FileOutputStream(new File(realPath,uploadFileName2)); //文件输出流
        OutputStream os3 = new FileOutputStream(new File(realPath,uploadFileName3)); //文件输出流
        OutputStream os4 = new FileOutputStream(new File(realPath,uploadFileName4)); //文件输出流
        OutputStream os5 = new FileOutputStream(new File(realPath,uploadFileName5)); //文件输出流

        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is1.read(buffer))!=-1){
            os1.write(buffer,0,len);
            os1.flush();
        }
        while ((len=is2.read(buffer))!=-1){
            os2.write(buffer,0,len);
            os2.flush();
        }
        while ((len=is3.read(buffer))!=-1){
            os3.write(buffer,0,len);
            os3.flush();
        }
        while ((len=is4.read(buffer))!=-1){
            os4.write(buffer,0,len);
            os4.flush();
        }
        while ((len=is5.read(buffer))!=-1){
            os5.write(buffer,0,len);
            os5.flush();
        }
        os1.close();
        os2.close();
        os3.close();
        os4.close();
        os5.close();
        is1.close();
        is2.close();
        is3.close();
        is4.close();
        is5.close();

        String img01="images/"+user.getUserCode()+"/"+uploadFileName1;
        String img02="images/"+user.getUserCode()+"/"+uploadFileName2;
        String img03="images/"+user.getUserCode()+"/"+uploadFileName3;
        String img04="images/"+user.getUserCode()+"/"+uploadFileName4;
        String img05="images/"+user.getUserCode()+"/"+uploadFileName5;

        productTeamService.addmerchandise(productcode,productname,productdetil,qwer,productpath,productgo,touring_party_code,productdetils,priductaddress,user.getUserCode(),img01,img02,img03,img04,img05,price,team_number,remain_number,group_number);







        return "addmerchandise";
    }

}
